package com.boot.web.exception;

import com.boot.web.http.HttpModel;
import com.boot.web.util.HttpResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * @author 霜寒 <1621856595@qq.com>
 * @description 全局异常处理
 * @date 2019/5/20 19:05
 **/

@Deprecated
@Slf4j
//@ControllerAdvice
//@ResponseBody
public class BootWebExceptionHandler {

    ExceptionHandlerManager manager;

    public BootWebExceptionHandler(ExceptionHandlerManager manager) {
        this.manager = manager;
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<HttpModel<Object>> handle(Exception e) {
        log.info("===================================== Handle Exception =====================================");
        log.warn("Exception      : {}", e.toString());
        log.warn("message        : {}", e.getMessage());
        if (log.isDebugEnabled()) {
            e.printStackTrace();
        }
        for (ModuleExceptionHandler handler : manager.getHandlers()) {
            if (handler.support(e)) {
                return handler.handle(e);
            }
        }
        return HttpResponseUtil.badRequest(e.getMessage());
    }

}
